package com.nageoffer.shortlink.admin.remote.dto;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.remote.dto.req.RecycleBinRecoverDTO;
import com.nageoffer.shortlink.admin.remote.dto.req.*;
import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.nageoffer.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface ShortLinkRemoteService {

    default Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO reqParam) {
        String resultBody = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(reqParam));
        return JSON.parseObject(resultBody, new TypeReference<>() {
        });
    }

    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO reqParam) {
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("gid", reqParam.getGid());
        reqMap.put("current", reqParam.getCurrent());
        reqMap.put("size", reqParam.getSize());
        String result = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", reqMap);
        return JSON.parseObject(result, new TypeReference<>() {
        });
    }


    default Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(List<String> requestParam) {
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("requestParam", requestParam);
        String result = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", reqMap);
        return JSON.parseObject(result, new TypeReference<>() {
        });
    }

    default Result<Void> updateShortLink(ShortLinkUpdateReqDTO reqParam) {
        String resultBody = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/update", JSON.toJSONString(reqParam));
        return JSON.parseObject(resultBody, new TypeReference<>() {
        });
    }

    default Result<String> getUrlTitle(String url) {
        String title = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title?url=" + url);
        return JSON.parseObject(title, new TypeReference<>() {
        });
    }

    default void saveRecycleBin(RecycleBinSaveDTO requestParam) {
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
    }

    default Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO reqParam) {
        Map<String, Object> reqMap = new HashMap<>();
        reqMap.put("gidList", reqParam.getGidList());
        reqMap.put("current", reqParam.getCurrent());
        reqMap.put("size", reqParam.getSize());
        String result = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/page", reqMap);
        return JSON.parseObject(result, new TypeReference<>() {
        });
    }

    default void recoverRecycleBin(RecycleBinRecoverDTO requestParam) {
        HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requestParam));
    }

    default void removeRecycleBin(RecycleBinRemoveDTO requestParam) {
       HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requestParam));
    }

}
